package com.chaoyer.cynet.common.domain;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class WebResult<T> {
	private long code = 200;

	private boolean success;

	private String msg = "";

	private String remark;
	
	private T data;

	public static <T> WebResult<T> success(T data) {
		return successRemark(data, null);
	}

	public static <T> WebResult<T> success() {
		return successRemark(null);
	}

	public static <T> WebResult<T> error(long code, String msg) {
		return errorRemark(code, msg, null);
	}

	public static <T> WebResult<T> error(long code, T data, String msg) {
		return errorRemark(code, data, msg, null);
	}

	public void setError(ErrorInfo error) {
		this.setCode(error.getCode());
		this.setMsg(error.getMsg());
	}

	public static <T> WebResult<T> error(ErrorInfo error) {
		return errorRemark(error, null, null);
	}

	public static <T> WebResult<T> error(ErrorInfo error, T data) {
		return errorRemark(error, data, null);
	}

	public static <T> WebResult<T> error(ErrorInfo error, T data, String remark) {
		return errorRemark(error, data, remark);
	}

	public static <T> WebResult<T> successRemark(T data, String remark) {
		return WebResult.<T>builder().code(200).success(true).data(data).remark(remark).build();
	}

	public static <T> WebResult<T> successRemark(T data, String msg, String remark) {
		return WebResult.<T>builder().code(200).success(true).data(data).remark(remark).msg(msg).build();
	}

	public static <T> WebResult<T> successRemark(String remark) {
		return WebResult.<T>builder().code(200).success(true).data(null).remark(remark).build();
	}

	public static <T> WebResult<T> errorRemark(long code, String msg, String remark) {
		return WebResult.<T>builder().code(code).success(false).data(null).msg(msg).remark(remark).build();
	}

	public static <T> WebResult<T> errorRemark(long code, T data, String msg, String remark) {
		return WebResult.<T>builder().code(code).success(false).data(data).msg(msg).remark(remark).build();
	}

	public static <T> WebResult<T> errorRemark(ErrorInfo error, T data, String remark) {
		return WebResult.<T>builder().code(error.getCode()).success(false).data(data).msg(error.getMsg())
				.remark(remark).build();
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
